package com.zxg.zbc.biz.util;

import java.util.ArrayList;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.zxg.util.WebUtil;

public class OAuthUtil {

	private final static Logger log = LoggerFactory.getLogger(OAuthUtil.class);

	static String state = "";
	static String scope = "item";
	static String view = "web";

	static String grant_type = "authorization_code";

	public static String getOAuthUrl() {
		return String.format(ApiConfigs.CODE_URL, ApiConfigs.APP_KEY, ApiConfigs.APP_INDEX_URL, state, scope, view);
	}

	public static OAuthToken accessToken(String code, String error, String error_description) {

		if (code == null) {
			return null;
		}
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new NameValuePair("client_id", ApiConfigs.APP_KEY));
		params.add(new NameValuePair("client_secret", ApiConfigs.APP_SECRET));
		params.add(new NameValuePair("grant_type", grant_type));

		params.add(new NameValuePair("code", code));
		params.add(new NameValuePair("redirect_uri", ApiConfigs.APP_INDEX_URL));

		params.add(new NameValuePair("scope", scope));
		params.add(new NameValuePair("state", state));
		params.add(new NameValuePair("view", view));

		String tokenJson = "";
		int cnt = 0;
		while ((cnt++) < 5 && StringUtils.isEmpty(tokenJson)) {
			tokenJson = WebUtil.post(ApiConfigs.TOKEN_URL, params);
			log.warn("access token doing for " + cnt + ", return tokenJson:"
					+ (StringUtils.isEmpty(tokenJson) ? "empty" : tokenJson));
		}

		if (StringUtils.isEmpty(tokenJson)) {
			return null;
		}
		try {
			return JSON.parseObject(tokenJson, OAuthToken.class);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public static void main(String[] args) {
		OAuthToken o = OAuthUtil.accessToken("aj4HLz2sGHFO5Bv2RNjnI6dq2742", "", "");
		System.out.println(o.toString());
	}

	@JsonAutoDetect
	public static class OAuthToken {

		@JsonProperty
		public long w2_expires_in;

		@JsonProperty
		public String taobao_user_id;

		@JsonProperty
		public String taobao_user_nick;

		@JsonProperty
		public long w1_expires_in;

		@JsonProperty
		public long re_expires_in;

		@JsonProperty
		public long r2_expires_in;

		@JsonProperty
		public long expires_in;

		@JsonProperty
		public String token_type;

		@JsonProperty
		public String refresh_token;

		@JsonProperty
		public String access_token;

		@JsonProperty
		public long r1_expires_in;

		@JsonProperty
		public long sub_taobao_user_id;

		@JsonProperty
		public String sub_taobao_user_nick;

		public long getSub_taobao_user_id() {
			return sub_taobao_user_id;
		}

		public String getSub_taobao_user_nick() {
			return sub_taobao_user_nick;
		}

		public void setSub_taobao_user_id(long sub_taobao_user_id) {
			this.sub_taobao_user_id = sub_taobao_user_id;
		}

		public void setSub_taobao_user_nick(String sub_taobao_user_nick) {
			this.sub_taobao_user_nick = sub_taobao_user_nick;
		}

		public long getW2_expires_in() {
			return w2_expires_in;
		}

		public void setW2_expires_in(long w2_expires_in) {
			this.w2_expires_in = w2_expires_in;
		}

		public String getTaobao_user_id() {
			return taobao_user_id;
		}

		public void setTaobao_user_id(String taobao_user_id) {
			this.taobao_user_id = taobao_user_id;
		}

		public String getTaobao_user_nick() {
			return taobao_user_nick;
		}

		public void setTaobao_user_nick(String taobao_user_nick) {
			this.taobao_user_nick = taobao_user_nick;
		}

		public long getW1_expires_in() {
			return w1_expires_in;
		}

		public void setW1_expires_in(long w1_expires_in) {
			this.w1_expires_in = w1_expires_in;
		}

		public long getRe_expires_in() {
			return re_expires_in;
		}

		public void setRe_expires_in(long re_expires_in) {
			this.re_expires_in = re_expires_in;
		}

		public long getR2_expires_in() {
			return r2_expires_in;
		}

		public void setR2_expires_in(long r2_expires_in) {
			this.r2_expires_in = r2_expires_in;
		}

		public long getExpires_in() {
			return expires_in;
		}

		public void setExpires_in(long expires_in) {
			this.expires_in = expires_in;
		}

		public String getToken_type() {
			return token_type;
		}

		public void setToken_type(String token_type) {
			this.token_type = token_type;
		}

		public String getRefresh_token() {
			return refresh_token;
		}

		public void setRefresh_token(String refresh_token) {
			this.refresh_token = refresh_token;
		}

		public String getAccess_token() {
			return access_token;
		}

		public void setAccess_token(String access_token) {
			this.access_token = access_token;
		}

		public long getR1_expires_in() {
			return r1_expires_in;
		}

		public void setR1_expires_in(long r1_expires_in) {
			this.r1_expires_in = r1_expires_in;
		}

		@Override
		public String toString() {
			return "OAuthToken [w2_expires_in=" + w2_expires_in + ", taobao_user_id=" + taobao_user_id
					+ ", taobao_user_nick=" + taobao_user_nick + ", w1_expires_in=" + w1_expires_in + ", re_expires_in="
					+ re_expires_in + ", r2_expires_in=" + r2_expires_in + ", expires_in=" + expires_in
					+ ", token_type=" + token_type + ", refresh_token=" + refresh_token + ", access_token="
					+ access_token + ", r1_expires_in=" + r1_expires_in + "]";
		}

	}
}
